package com.heu.blood.security.controller;

import com.heu.blood.common.Constant.UserConstantData;
import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.security.entity.UserEntity;
import com.heu.blood.security.service.UserService;
import com.heu.blood.security.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;



/**
 *
 * @author lrz
 * @email lrz@heu.com
 * @date 2023-08-16 02:58:36
 */
@RestController
@RequestMapping("security/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "用户模块", operate = "分页查询用户信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "用户模块", operate = "查询用户信息")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "用户模块", operate = "保存用户信息")
    public R save(@RequestBody UserEntity user){
        String res =  userService.saveUser(user);
        return R.ok(res);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "用户模块", operate = "修改用户信息")
    public R update(@RequestBody UserEntity user){
		String msg = userService.updateUserById(user);

        return R.ok(msg);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "用户模块", operate = "删除用户信息")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 验证账号和密码
     * @param username 用户名
     * @param password 密码
     * @return 正确或错误
     */
    @GetMapping("/testUser")
    @ControllerLogAnnotation(module = "用户模块", operate = "验证账号和密码")
    public R testLogin(String username, String password){
        String msg = userService.testLogin(username, password);
        return R.ok();
    }

    /**
     *
     * @param user 用户
     * @return 结果
     */
    @RequestMapping("/setSignUrl")
    @ControllerLogAnnotation(module = "用户模块", operate = "保存用户签名")
    public R testLogin(@RequestBody UserVo user){
        int flag = userService.setSignUrl(user);
        return R.ok();
    }

}
